/***************************************************************************
 *  Filename: processmodel.cpp
 *  Class: ProcessModel
 *
 *  Copyright 2012  Xander van Kooten
 *  <xandervk@gmail.com>
 *
 *  Copyright 2012  Mario Henrique Voorsluys
 *  <forcaeluz@gmail.com>
 *
 ****************************************************************************/

/*
 *
 * This file is part of Parsax-Remote-Controller.
 *
 *    Parsax-Remote-Controller is free software: you can redistribute it
 *    and/or modify it under the terms of the GNU General Public License
 *    as published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    Parsax-Remote-Controller is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Parsax-Remote-Controller.
 *    If not, see <http://www.gnu.org/licenses/>.
 */

#include "processmodel.h"

ProcessModel::ProcessModel(bool gen, QObject *parent) :
    QAbstractTableModel(parent)
{
    isGeneral = gen;
}

int ProcessModel::rowCount(const QModelIndex &parent) const{
    return processes.size();
}

int ProcessModel::columnCount(const QModelIndex &parent) const{
    if(isGeneral){
        return 4;
    }else{
        return 3;
    }
}

QVariant ProcessModel::headerData(int section, Qt::Orientation orientation, int role) const{
    if(!isGeneral){
        section++;
    }
    if(orientation==Qt::Horizontal){
        switch(role){
        case Qt::DisplayRole:
            switch(section){
            case 0:
                return "Computer Name";
                break;
            case 1:
                return "Program ID";
                break;
            case 2:
                return "Program";
                break;
            case 3:
                return "Status";
                break;
            }
        }
    }
    return QVariant();
}

QVariant ProcessModel::data(const QModelIndex &index, int role) const{
    if(!index.isValid()){
        return QVariant();
    }
    int row = index.row();
    int column = index.column();
    if(!isGeneral){
        column++;
    }

    QString status = processes.at(row)->getStatus();
    QPixmap statusPixmap(30, 30);
    if(status=="nostatus"){
        statusPixmap.fill(QColor("orange"));
    }else if(status=="running"){
        statusPixmap.fill(QColor("green"));
    }else if(status=="notrunning"){
        statusPixmap.fill(QColor("red"));
    }else if(status=="starting"){
        statusPixmap.fill(QColor("blue"));
    }else{
        statusPixmap.fill(QColor("orange"));
    }
    QString niceStatusMessage;
    if(status=="nostatus"){
        niceStatusMessage = "No status received since connected";
    }else if(status=="running"){
        niceStatusMessage = "The program is running";
    }else if(status=="notrunning"){
        niceStatusMessage = "The program is not running";
    }else if(status=="starting"){
        niceStatusMessage = "The program is starting";
    }else if(status=="disconnected"){
        niceStatusMessage = "No connection with slave";
    }else{
        niceStatusMessage = "Unkown status: " + status;
    }

    switch(role){
    case Qt::DisplayRole:
        switch(column){
        case 0:
            return processes.at(row)->getPc();
            break;
        case 1:
            return processes.at(row)->getId();
            break;
        case 2:
            return processes.at(row)->getProgram();
            break;
        case 3:
            return "";
            break;
        }
        break;
    case Qt::DecorationRole:
        switch(column){
        case 3:
            return statusPixmap;
            break;
        }
        break;
    case Qt::SizeHintRole:
        switch(column){
        case 3:
            return QSize(40, 40);
            break;
        }
        break;
    case Qt::ToolTipRole:
        switch(column){
        case 3:
            return niceStatusMessage;
            break;
        }
        break;
    case 33:
        return qVariantFromValue((void *) processes.at(row));
    }

    return QVariant();
}

void ProcessModel::addProcess(RemoteProcess *proc){
    beginResetModel();
    processes.append(proc);
    connect(proc, SIGNAL(infoUpdated()), SLOT(processesUpdated()));
    endResetModel();
}

void ProcessModel::removeProcess(RemoteProcess *proc){
    beginResetModel();
    processes.removeAll(proc);
    disconnect(proc, SIGNAL(infoUpdated()), this, SLOT(processesUpdated()));
    endResetModel();
}

void ProcessModel::processesUpdated(){
    QModelIndex start;
    QModelIndex stop(start.child(rowCount(start)-1, columnCount(start)-1));
    emit dataChanged(start, stop);
}
